package com.plus.bootmybatisplus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plus.bootmybatisplus.entity.Book;
import com.plus.bootmybatisplus.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    BookService bookService;

    @GetMapping
    public String books(Model model){
        List<Book> list = bookService.list();
        model.addAttribute("books",list);
        return "books";
    }

    @PostMapping
    public String searchBook(Book book,Model model,String name,String category,String author){
       List<Book> list = bookService.searchBook(book);
       model.addAttribute("books",list);
        model.addAttribute("name",name);
        model.addAttribute("category",category);
        model.addAttribute("author",author);
       return "books";
    }

     @GetMapping("/page")
    public String page(@RequestParam(defaultValue = "1") Integer pageNum, Model model){
        IPage page = bookService.getPage(pageNum);
        model.addAttribute("page",page);
        return "bookPage";
     }
}
